﻿// Decompiled with JetBrains decompiler
// Type: FlamethrowerSpell.FlamethrowerMerge
// Assembly: FlamethrowerSpell, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: FE877AF4-189C-4F20-BE78-CAD168BEA322
// Assembly location: E:\Temp\Flamethrower Spell\FlamethrowerSpell.dll

using System.Collections.Generic;
using ThunderRoad;
using UnityEngine;

namespace FlamethrowerSpell
{
  public class FlamethrowerMerge : SpellMergeData
  {
    private EffectData effect;
    private EffectInstance instance;
    private StatusDataBurning burning;
    private float lastTick = Time.time;
    public bool active;
    public GameObject origin;
    public HashSet<(ThunderEntity entity, UnityEngine.Vector3 point)> entities = new HashSet<(ThunderEntity, UnityEngine.Vector3)>();

    public override void Load(Mana mana)
    {
      base.Load(mana);
      this.effect = Catalog.GetData<EffectData>("Flamethrower");
      this.burning = Catalog.GetData<StatusDataBurning>("Burning");
      this.origin = new GameObject("Origin");
    }

    public override void Unload()
    {
      base.Unload();
      this.instance?.End();
      this.entities.Clear();
      if (!this.mana.creature.isPlayer)
        return;
      Player.local.physicRangeModifier.Remove((object) this);
    }

    public override void Merge(bool active)
    {
      base.Merge(active);
      this.instance?.End();
      this.entities.Clear();
      if (this.mana.creature.isPlayer)
        Player.local.physicRangeModifier.Remove((object) this);
      this.active = false;
    }

    public override void FixedUpdate()
    {
      base.FixedUpdate();
      if (!this.active || !this.mana.creature.isPlayer || this.mana.creature.currentLocomotion.isGrounded)
        return;
      this.mana.casterLeft.ragdollHand.physicBody.AddForce(-UnityEngine.Vector3.Slerp(this.mana.casterLeft.magicSource.up, this.mana.casterRight.magicSource.up, 0.5f) * FlamethrowerManager.MergePlayerForce, ForceMode.Acceleration);
      this.mana.casterRight.ragdollHand.physicBody.AddForce(-UnityEngine.Vector3.Slerp(this.mana.casterLeft.magicSource.up, this.mana.casterRight.magicSource.up, 0.5f) * FlamethrowerManager.MergePlayerForce, ForceMode.Acceleration);
    }

    public override void Update()
    {
      base.Update();
      this.origin.transform.SetPositionAndRotation(this.mana.mergePoint.position, Quaternion.LookRotation(UnityEngine.Vector3.Slerp(this.mana.casterLeft.magicSource.up, this.mana.casterRight.magicSource.up, 0.5f)));
      UnityEngine.Vector3 vector3;
      int num1;
      if ((double) UnityEngine.Vector3.SignedAngle(UnityEngine.Vector3.Slerp(this.mana.casterLeft.magicSource.up, this.mana.casterRight.magicSource.up, 0.5f), this.mana.casterLeft.magicSource.up, UnityEngine.Vector3.Cross(this.mana.creature.centerEyes.position - this.origin.transform.position, this.mana.casterLeft.magicSource.position - this.origin.transform.position).normalized) < -20.0)
      {
        UnityEngine.Vector3 from = UnityEngine.Vector3.Slerp(this.mana.casterLeft.magicSource.up, this.mana.casterRight.magicSource.up, 0.5f);
        UnityEngine.Vector3 up = this.mana.casterRight.magicSource.up;
        vector3 = UnityEngine.Vector3.Cross(this.mana.casterRight.magicSource.position - this.origin.transform.position, this.mana.creature.centerEyes.position - this.origin.transform.position);
        UnityEngine.Vector3 normalized = vector3.normalized;
        num1 = (double) UnityEngine.Vector3.SignedAngle(from, up, normalized) > 20.0 ? 1 : 0;
      }
      else
        num1 = 0;
      if (num1 != 0)
      {
        if (!this.active && (double) this.currentCharge >= 0.800000011920929)
        {
          this.active = true;
          this.instance?.End();
          this.instance = this.effect.Spawn(this.origin.transform, (CollisionInstance) null, true, (ColliderGroup) null, false, 1f, 1f);
          this.instance.SetIntensity(1f);
          this.instance.Play();
          foreach (ThunderBehaviour effect in this.instance.effects)
            effect.transform.localScale = UnityEngine.Vector3.one * 0.5f * FlamethrowerManager.MergeScale;
          if (this.mana.creature.isPlayer)
            Player.local.physicRangeModifier.Add((object) this, 6f * FlamethrowerManager.MergeScale * this.GetModifier(Modifier.Range));
        }
      }
      else if (this.active)
      {
        this.active = false;
        this.instance?.End();
        this.entities.Clear();
        if (this.mana.creature.isPlayer)
          Player.local.physicRangeModifier.Remove((object) this);
      }
      if (!this.active || (double) Time.time - (double) this.lastTick < 0.30000001192092896)
        return;
      this.lastTick = Time.time;
      float num2 = 6f * FlamethrowerManager.MergeScale;
      RaycastHit hitInfo;
      if (Physics.Raycast(new Ray(this.origin.transform.position, this.origin.transform.forward), out hitInfo, 6f * FlamethrowerManager.MergeScale, 5201, QueryTriggerInteraction.Ignore))
        num2 = hitInfo.distance;
      float num3 = (float) ((double) num2 * 1.0 / 5.0);
      float radius1 = (float) ((double) num2 * 1.0 / 5.0);
      float num4 = (float) ((double) num2 * 3.0 / 5.0);
      float radius2 = (float) ((double) num2 * 2.0 / 5.0);
      this.entities.Clear();
      ThunderEntity.InRadiusClosestPoint(this.origin.transform.position + this.origin.transform.forward * num3, radius1, allocList: (ICollection<(ThunderEntity, UnityEngine.Vector3)>) this.entities);
      ThunderEntity.InRadiusClosestPoint(this.origin.transform.position + this.origin.transform.forward * num4, radius2, allocList: (ICollection<(ThunderEntity, UnityEngine.Vector3)>) this.entities);
      foreach ((ThunderEntity entity, UnityEngine.Vector3 point) in this.entities)
      {
        if ((Object) entity != (Object) this.mana.creature)
        {
          ThunderEntity thunderEntity = entity;
          vector3 = point - this.origin.transform.position;
          UnityEngine.Vector3 force = vector3.normalized * FlamethrowerManager.MergeForce;
          thunderEntity.AddForce(force, ForceMode.Acceleration);
          entity.Inflict((StatusData) this.burning, (object) this, parameter: (object) 100f);
          Creature creature1 = entity as Creature;
          if ((Object) creature1 != (Object) null)
          {
            if (!entity.IsBurning)
            {
              Creature creature2 = creature1;
              vector3 = point - this.origin.transform.position;
              UnityEngine.Vector3 direction = vector3.normalized * FlamethrowerManager.MergeForce;
              creature2.TryPush(Creature.PushType.Magic, direction, 3);
            }
            creature1.Damage(FlamethrowerManager.MergeDamage, DamageType.Fire);
          }
        }
      }
    }
  }
}
